<?php

/**
 * @author    1054229380@qq.com
 * @since    2017-7-3
 * @desc 路由类
 **/
class router
{
    var $url = NULL;
    var $classDirNum = 0;

    function __construct()
    {
        if ($this->url == "") {
            $this->url = $this->getURL();
        }
    }

    /**
     * 获取路由URL
     * @return string
     **/
    public function getURL()
    {
        if ($this->url != "") {
            return $this->url;
        }
        $uri = $_SERVER['PHP_SELF'];
        $url = str_replace($_SERVER['SCRIPT_NAME'], '', $uri);
        $paraArr = explode("?", $url);
        $paraNum = count($paraArr) - 1;
        if ($paraNum == "0") {
            $currUrl = $url;
        } else if ($paraNum == "1") {
            $currUrl = $paraArr[0];
        } else {
            $currUrl = $paraArr[1];
        }
        $this->url = $currUrl;
        return $currUrl;
    }

    /**
     * getController
     * @return string
     **/
    public function getController()
    {
        $url = $this->url;
        $x = explode('/', $url);
        $_locationNum = $this->classDirNum + 1;
        $class = $x[$_locationNum];
        if ($class == "") {
            $class = "index";
        }
        return $class;
    }

    /**
     * getAction
     * @return string
     **/
    public function getAction()
    {
        $url = $this->url;
        $x = explode('/', $url);
        $_locationNum = $this->classDirNum + 2;
        $method = $x[$_locationNum];
        if ($method == "") {
            $method = "main";
        }
        return $method;
    }

    /**
     * getParas
     * @return array
     **/
    public function getParas()
    {
        $weburl = $this->url;
        $repArr = array('\'', '.html');
        $weburl = str_replace($repArr, '', $weburl);
        $pArr = explode('/', $weburl);
        $paraNum = count($pArr);
        $how = 1;
        $_locationNum = $this->classDirNum + 3;
        $tempArr = array();
        for ($i = $_locationNum; $i < $paraNum; $i++) {
            $pValue = $pArr[$i];
            $tempArr[$how] = $pValue;
            $how = $how + 1;
        }
        return $tempArr;
    }
}

?>